package com.coldwindblows.blog.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coldwindblows.blog.dao.OptionMapper;
import com.coldwindblows.blog.pojo.OptionDo;
import com.coldwindblows.blog.service.IOptionService;

/**
 * options表的service
 * Created by ColdWindBlows on 2018/07/04
 *
 */
@Service
public class IOptionServiceImpl implements IOptionService {

	@Autowired
	private OptionMapper optionDao;
	
	@Override
	public boolean insertOption(OptionDo option) {
		return optionDao.insert(option) > 0;
	}

	@Transactional
	@Override
	public boolean insertOption(String name, String value) {
		OptionDo option = new OptionDo();
		option.setName(name);
		option.setValue(value);
		if(optionDao.selectByName(name) == null) {
			return optionDao.insert(option) > 0;
		} else {
			return optionDao.updateByPrimaryKey(option) > 0;
		}
	}

	@Transactional
	@Override
	public boolean insertOptions(Map<String, String> options) {
		if(null != options && !options.isEmpty()) {
			options.forEach(this::insertOption);
		}
		return false;
	}

	@Override
	public List<OptionDo> getOptions() {
		return optionDao.listAll();
	}

	@Override
	public OptionDo getDetail(String name) {
		return optionDao.selectByName(name);
	}

}
